﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using MxWeiXinPF.Common;
using System.Web.UI.HtmlControls;
using System.Data;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class indexcatepic_edit : ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info;
        private int id = 0;
        protected int shopid = 0;
        protected int wid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.action == MXEnums.ActionEnum.Edit.ToString())
                {
                    base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.Edit.ToString());
                    if (!this.DoEdit(this.id))
                    {
                        base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
                    }
                    else
                    {
                        base.JscriptMsg("修改成功！", "indexcatepic_list.aspx?shopid=" + this.shopid, "Success");
                    }
                }
                else
                {
                    base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.Add.ToString());
                    if (!this.DoAdd())
                    {
                        base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
                    }
                    else
                    {
                        base.JscriptMsg("添加成功！", "indexcatepic_list.aspx?shopid=" + this.shopid, "Success");
                    }
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private bool DoAdd()
        {
            bool flag = false;
            MxWeiXinPF.Model.indexcatepic model = new MxWeiXinPF.Model.indexcatepic();
            MxWeiXinPF.BLL.indexcatepic indexcatepic2 = new MxWeiXinPF.BLL.indexcatepic();
            model.shopid = this.shopid;
            model.title = this.txtTitle.Text.Trim();
            model.link_url = this.txtLinkUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text;
            int num = int.Parse(this.ddlCategoryId.SelectedItem.Value);
            model.categoryid = new int?(num);
            model.isview = "1";
            if (this.wid > 0)
            {
                model.wid = new int?(this.wid);
            }
            else
            {
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                model.wid = new int?(weiXinCode.id);
            }
            if (this.shopid > 0)
            {
                model.shopid = this.shopid;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            model.content = this.txtContent.Value;
            if (this.txtContent.Value.Trim().Length > 100)
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, 100);
            }
            else
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, this.txtContent.Value.Trim().Length);
            }
            model.sort_id = Utils.StrToInt(this.txtSortId.Text.Trim(), 0x63);
            if (indexcatepic2.Add(model) > 0)
            {
                base.AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加首页广告图:" + model.title);
                flag = true;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = false;
            MxWeiXinPF.BLL.indexcatepic indexcatepic = new MxWeiXinPF.BLL.indexcatepic();
            MxWeiXinPF.Model.indexcatepic model = indexcatepic.GetModel(_id);
            if (this.shopid > 0)
            {
                model.shopid = this.shopid;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            if (this.wid > 0)
            {
                model.wid = new int?(this.wid);
            }
            else
            {
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                model.wid = new int?(weiXinCode.id);
            }
            model.title = this.txtTitle.Text.Trim();
            model.link_url = this.txtLinkUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text;
            model.content = this.txtContent.Value;
            int num = int.Parse(this.ddlCategoryId.SelectedItem.Value);
            model.categoryid = new int?(num);
            model.isview = "1";
            if (this.txtContent.Value.Trim().Length > 100)
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, 100);
            }
            else
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, this.txtContent.Value.Trim().Length);
            }
            model.sort_id = Utils.StrToInt(this.txtSortId.Text.Trim(), 0x63);
            model.update_time = new DateTime?(DateTime.Now);
            if (indexcatepic.Update(model))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改首页广告图:" + model.title);
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string queryString = MXRequest.GetQueryString("action");
                if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
                {
                    this.action = MXEnums.ActionEnum.Edit.ToString();
                    this.id = MXRequest.GetQueryInt("id");
                    if (this.id == 0)
                    {
                        base.JscriptMsg("传输参数不正确！", "back", "Error");
                        return;
                    }
                    if (!new MxWeiXinPF.BLL.indexcatepic().Exists(this.id))
                    {
                        base.JscriptMsg("记录不存在或已被删除！", "back", "Error");
                        return;
                    }
                }
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.View.ToString());
                    Model.manager weiXinCode = base.GetAdminInfo();
                    this.shopid = weiXinCode.id;
                    if (this.shopid == 0)
                    {
                        this.shopid = weiXinCode.parentid;
                    }
                    this.wid = weiXinCode.parentid;
                    this.TreeBind();
                    if (this.action == MXEnums.ActionEnum.Edit.ToString())
                    {
                        this.ShowInfo(this.id);
                    }
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private void ShowInfo(int _id)
        {
            MxWeiXinPF.Model.indexcatepic model = new MxWeiXinPF.BLL.indexcatepic().GetModel(_id);
            if (model.categoryid.HasValue && (model.categoryid.ToString() != null))
            {
                this.ddlCategoryId.SelectedValue = model.categoryid.ToString();
            }
            this.txtTitle.Text = model.title;
            this.txtLinkUrl.Text = model.link_url;
            this.txtImgUrl.Text = model.img_url;
            this.txtContent.Value = model.content;
            this.txtSortId.Text = model.sort_id.ToString();
            if (model.shopid > 0)
            {
                this.shopid = model.shopid;
            }
            if (model.wid > 0)
            {
                this.wid = model.wid.Value;
            }
        }

        private void TreeBind()
        {
            MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
            DataTable wCodeList = new MxWeiXinPF.BLL.wx_shop_category().GetWCodeList(weiXinCode.parentid, 0);
            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("请选择类别...", ""));
            foreach (DataRow row in wCodeList.Rows)
            {
                string str = row["id"].ToString();
                int num = int.Parse(row["class_layer"].ToString());
                string text = row["title"].ToString().Trim();
                if (num == 1)
                {
                    this.ddlCategoryId.Items.Add(new ListItem(text, str));
                }
                else
                {
                    text = "├ " + text;
                    text = Utils.StringOfChar(num - 1, "　") + text;
                    this.ddlCategoryId.Items.Add(new ListItem(text, str));
                }
            }
        }
    }
}